/**
 * Moondance UML modeling tool
 * 
 * @author Dascalu Laurentiu (dascalu.laurentziu@gmail.com).
 * 
 * This program is free software; you can redistribute it and
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ro.pub.cs.se.moondance.xmi.converter;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import ro.pub.cs.se.moondance.MoondanceData;

public class UMLAggregationConnectionXMI extends UMLConnectionXMI {
	protected String aggregationType;

	public UMLAggregationConnectionXMI(String name, String id, String source,
			String destination, String sourceLabel, String destinationLabel,
			String upperSourceType, String lowerSourceType, String upperSourceValue,
			String lowerSourceValue, String upperDestinationType, String lowerDestinationType,
			String upperDestinationValue, String lowerDestinationValue) {
		super(name, XMICommon.UML_ASSOCIATION, id, source, destination,
				sourceLabel, destinationLabel, upperSourceType, lowerSourceType,
				upperSourceValue, lowerSourceValue,
				upperDestinationType, lowerDestinationType,
				upperDestinationValue, lowerDestinationValue);
		aggregationType = XMICommon.SHARED;
	}

	/**
	 * Example for AGGREGATION
	 * 
	 * <packagedElement
	 * xmi:type="uml:Association"
	 * xmi:id="_L-BKYGcmEd6eLuBoYJyKmQ"
	 * memberEnd="_L-K7YGcmEd6eLuBoYJyKmQ _L-K7Y2cmEd6eLuBoYJyKmQ">
	 *     <ownedEnd
	 *         xmi:id="_L-K7YGcmEd6eLuBoYJyKmQ"
	 *         name="src"
	 *         type="_HIolQGclEd6eLuBoYJyKmQ" // source
	 *         association="_L-BKYGcmEd6eLuBoYJyKmQ">
	 *             <upperValue
	 *                 xmi:type="uml:LiteralUnlimitedNatural"
	 *                 xmi:id="_L-K7YmcmEd6eLuBoYJyKmQ"
	 *                 value="10"/>
	 *             <lowerValue
	 *                 xmi:type="uml:LiteralInteger"
	 *                 xmi:id="_L-K7YWcmEd6eLuBoYJyKmQ"
	 *                 value="9"/>
	 *     </ownedEnd>
	 *     
	 *     <ownedEnd
	 *         xmi:id="_L-K7Y2cmEd6eLuBoYJyKmQ"
	 *         name="dst"
	 *         type="_G6e0gGclEd6eLuBoYJyKmQ" // target
	 *         aggregation="shared"
	 *         association="_L-BKYGcmEd6eLuBoYJyKmQ">
	 *             <upperValue
	 *                 xmi:type="uml:LiteralUnlimitedNatural"
	 *                 xmi:id="_L-K7ZWcmEd6eLuBoYJyKmQ"
	 *                 value="n"/>
	 *             <lowerValue
	 *                 xmi:type="uml:LiteralInteger"
	 *                 xmi:id="_L-K7ZGcmEd6eLuBoYJyKmQ"
	 *                 value="1"/>
	 *     </ownedEnd>
	 * </packagedElement>
	 */

	@Override
	public void serialize(Element root, Document document) {
		Element elementAggregation = document.createElement(XMICommon.PACKED_ELEMENT);
		elementAggregation.setAttribute(XMICommon.XMI_TYPE, type);
		
		String aggregationID = MoondanceData.newRandomString();
		elementAggregation.setAttribute(XMICommon.XMI_ID, aggregationID);
		elementAggregation.setAttribute(XMICommon.NAME, name);
		
		String generatedSourceID = MoondanceData.newRandomString();
		String generatedDestinationID = MoondanceData.newRandomString();
		
		elementAggregation.setAttribute(XMICommon.MEMBER_END,
				generatedSourceID + " " + generatedDestinationID);
		
		Element sourceElement = document.createElement(XMICommon.OWNED_END);
		serialize(root, document, sourceElement, generatedSourceID, aggregationID, source);
		serialize(root, document, sourceElement, MoondanceData.newRandomString(),
				MoondanceData.newRandomString(), upperSourceType, lowerSourceType,
				upperSourceValue, lowerSourceValue);
	
		Element destinationElement = document.createElement(XMICommon.OWNED_END);
		destinationElement.setAttribute(XMICommon.AGGREGATION, aggregationType);
		serialize(root, document, destinationElement, generatedDestinationID,
				aggregationID, destination);
		serialize(root, document, destinationElement, MoondanceData.newRandomString(),
				MoondanceData.newRandomString(), upperDestinationType, lowerDestinationType,
				upperDestinationValue, lowerDestinationValue);
		
		elementAggregation.appendChild(sourceElement);
		elementAggregation.appendChild(destinationElement);
		root.appendChild(elementAggregation);
	}
}
